package com.zhyl.demo.controller;

import com.zhyl.demo.entity.Specialty;
import com.zhyl.demo.mapper.SpecialtyMapper;
import com.zhyl.demo.service.SpecialtyService;
import com.zhyl.demo.utils.Result;
import com.zhyl.demo.utils.menuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yang3
 */
@RestController
@RequestMapping("/specialty")
public class SpecialtyController {

    private final SpecialtyService specialtyService;

    @Autowired
    public SpecialtyController(SpecialtyService specialtyService){
        this.specialtyService = specialtyService;
    }

    @RequestMapping("/selectList")
    public List<Specialty> selectList(){
        ArrayList<Specialty> specialties = specialtyService.selectList();
        return new menuTree(specialties).buildTree();
    }

    @RequestMapping("/addNewSpecialty")
    public Result addNewSpecialty(@RequestBody Specialty specialty){
        if(specialty==null){
            return Result.error("-1","参数错误");
        }
        Boolean b = specialtyService.addNewSpecialty(specialty);
        if (b){
            return Result.success("添加成功");
        }else
        {
            return Result.error("-1","添加失败");
        }
    }

    @RequestMapping("/updateSpecialty")
    public Result updateSpecialty(@RequestBody Specialty specialty){
        if(specialty==null){
            return Result.error("-1","参数错误");
        }
        Boolean b = specialtyService.updateSpecialty(specialty);
        if (b){
            return Result.success("修改成功");
        }else
        {
            return Result.error("-1","修改失败");
        }
    }

    @RequestMapping("/deleteSpecialty")
    public Result deleteSpecialty(Integer specialtyId){
        if (specialtyId==null)
        {
            return Result.error("-1","参数错误");
        }
        Boolean b = specialtyService.deleteSpecialty(specialtyId);
        if (b){
            return Result.success("删除成功");
        }else
        {
            return Result.error("-1","删除失败");
        }
    }
}
